<?php
namespace App\Services\Wxxcx;

use Illuminate\Support\Facades\DB;

class AdService extends BaseService
{
    /**
     * 广告
     */

    /**
     * 列表
     * @param array $pages【start，limit】
     */
    public function getList($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbAdList)->where('del', 1);
        if (isset($pas['uid']) && $pas['uid']) {
            $db = $db->where('uid', $pas['uid']);
        }
        $total = $db->count();
        $res = $db->orderBy('id', 'desc')
            ->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $lastOne = (array) $this->dbConn($this->dbAdUsed)
                ->where('ad_id', $v['id'])
                ->first();
            $arys[] = [
                'id' => $v['id'],
                'prefixUrl' => env('DOMAIN').'/',
                'thumb' => file_exists($v['thumb']) ? $v['thumb'] : '',
                'link' => $v['link'],
                'total' => $v['total'],
                'num' => $v['num'],
                'lasttime' => !empty($lastOne) ? $lastOne['addtime'] : $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }
}